# coding=utf-8
"""
设备信息之间数据的校验, redis必须以mysql为基础
"""
import redis
from dbtool import MysqlMgr

config_dev = {'host': '192.168.1.36',  # 默认127.0.0.1
              'user': 'root',
              'password': 'jsecode123',
              'port': 3306,  # 默认即为3306
              'database': 'rescue_platform_data',
              'charset': 'utf8'  # 默认即为utf8
              }

config_ali = {'host': '127.0.0.1',  # 默认127.0.0.1
              'user': 'jsecode',
              'password': 'jsecode123',
              'port': 13306,  # 默认即为3306
              'database': 'rescue_platform_data',
              'charset': 'utf8'  # 默认即为utf8
              }
#redis_dev = redis.StrictRedis(host='192.168.1.36', port=6379, password='Jsecode123', db=1)
redis_ali = redis.StrictRedis(host='localhost', port=16379, password='Jsecode123')
#redis_test = redis.StrictRedis(host='221.226.92.222', port=16379, password='Jsecode123')


def query_deviceinfo_mysql(**kwargs):
    """
    查询设备信息
    :param args:
    :param kwargs: 配置信息
    :return: list of (device_no, VEH_ID, DEVICE_TYPE,...)
    """
    cnn = MysqlMgr.Database(**kwargs)
    sqlQuery = 'select device_no, VEH_ID, DEVICE_TYPE from device_info order by device_no'
    result = cnn.fetch_all(sqlQuery, None)
    #print result

    cnn.close()
    return result

def query_deviceinfo_redis(redis):
    """
    查询 设备信息
    :param redis:
    :return:
    """
    key = "Device:BaseInfo"
    return redis.hgetall(key)

def save_deviceinfo_redis(redis, values):
    """
    查询 设备信息
    :param redis:
    :return:
    """
    key = "Device:BaseInfo"
    result = redis.hmset(key, values)
    print(result)
    print(redis.incr("Device:Version"))


def del_device_field_redis(redis, field):
    key = "Device:BaseInfo"
    print(redis.hdel(key, field))

def to_device(values):
    """只抽取设备列表"""
    result = []
    for item in values:
        result.append(item[0])

    return result

def to_redismap(values):
    """ key:deviceid
    value: {"vehid": 169,"type": 1}
    """
    result = {}
    for item in values:
        content = '{' + str.format('"vehid":{},"type":{}',item[1],item[2]) + '}'
        #print content
        result[item[0]] = content

    return result

#---------------
#保存到redis中
def sync_deviceinfo_to_redis(redis):
    dev_list = query_deviceinfo_mysql(**config_ali)
    print(len(dev_list))
    values = to_redismap(dev_list)
    save_deviceinfo_redis(redis, values)
    print("save to redis", values)

# 检查阿里云的数据一致, 并且删除不一致的
def aliyun_deviceinfo_check():
    dev_list = query_deviceinfo_mysql(**config_ali)
    devices = to_device(dev_list)
    myredis = redis_ali
    dev_list2 = query_deviceinfo_redis(myredis)
    #print (dev_list2)
    print("sql:", len(dev_list), ", redis:", len(dev_list2))
    for (k, v) in dev_list2.items():
        if k not in devices:
            #del_device_field_redis(myredis, k)
            print(k, v)


if __name__=="__main__":
    # sync_deviceinfo_to_redis(redis_ali)
    # del_device_field_redis(redis_dev, '3870010072')
    aliyun_deviceinfo_check()

